/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[欢迎加盟知启蒙，一起邂逅框架梦]
 * 
 * https://www.zhiqim.com/gitcan/zhiqim/zhiqim_account.htm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.zhiqim.account.action.zhima;

import org.zhiqim.account.ZacSessionUser;
import org.zhiqim.account.api.pay.AlipayServer;
import org.zhiqim.account.dbo.user.UserCertification;
import org.zhiqim.account.dbo.user.UserInfo;
import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.core.Action;
import org.zhiqim.kernel.Global;
import org.zhiqim.orm.ZTable;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.orm.dbo.Updater;

import com.alipay.api.response.ZhimaCustomerEpCertificationQueryResponse;

public class ZhimaEpCertifyReturnAction implements Action
{

    @Override
    public void execute(HttpRequest request) throws Exception
    {
        ZacSessionUser sessionUser = request.getSessionUser(ZacSessionUser.class);
        
        Selector sel = new Selector();
        sel.addMaybe("userId", sessionUser.getUserId());
        sel.addOrderbyDesc("certificationEndTime");
        UserCertification item = Global.get(ZTable.class).item(UserCertification.class, sel);
        if (item == null)
        {
            request.setRedirect("/account/authenticate.htm");
            return;
        }
        
        ZhimaCustomerEpCertificationQueryResponse resp = Global.getService(AlipayServer.class).epCertificationQuery(item.getBizNo());
        if (resp.isSuccess() && "true".equals(resp.getPassed()))
        {
            Updater updater = new Updater();
            updater.addMust("transactionId", item.getTransactionId());
            updater.addField("certificationStatus", 1);
            Global.get(ZTable.class).update(UserCertification.class, updater);

            Updater updater2 = new Updater();
            updater2.addMust("userId", sessionUser.getUserId());
            updater2.addField("certStatus", 1);
            updater2.addField("certName", item.getCertName());
            updater2.addField("certNo", item.getCertNo());
            updater2.addField("epCertType", item.getEpCertType());
            updater2.addField("epCertName", item.getEpCertName());
            updater2.addField("epCertNo", item.getEpCertNo());
            Global.get(ZTable.class).update(UserInfo.class, updater2);
        }
        else
        {
            Updater updater = new Updater();
            updater.addMust("transactionId", item.getTransactionId());
            updater.addField("certificationStatus", 2);
            Global.get(ZTable.class).update(UserCertification.class, updater);
        }
        
        request.setRedirect("/account/authenticate.htm");
    }

}
